let bookBtn = document.querySelectorAll(".registered_btn> div > button");
let nameMsg = document.querySelector(".name_msg > input");
let phoneMsg = document.querySelector(".phone_msg > input")
let idMsg = document.querySelector(".id_msg > input")


bookBtn.forEach((item, index) => {
    // console.log(item);
    if (index == 1) {
        // 开始预约
        item.addEventListener("click", () => {
            // ajaxFn();
            // location.href = "booking.html"

            let name = nameMsg.value;
            let phone = phoneMsg.value;
            let card = idMsg.value;
            console.log(name, phone, card);

            // 设置ajax参数
            let api = "http://newshopapi.0melon0.cn/api/f_user/register"
            let data = {
                "name": name,
                "phone": phone,
                "card": card
            }

            // 请求数据
            ajax(api, data, "POST").then(res => {
                // 存储数据
                sessionStorage.setItem("token", res.result.token);
                sessionStorage.setItem("name", name);
                sessionStorage.setItem("phone", phone);
                sessionStorage.setItem("card", card);
                location.href = "booking.html"
            }).catch(err => {
                console.log(err);
            })


        });



    } else {
        // 我的预约
        item.addEventListener("click", () => {
            // ajaxFn();
            // location.href = "check.html"

            // 获取用户输入信息
            let name = nameMsg.value;
            let phone = phoneMsg.value;
            let card = idMsg.value;
            console.log(name, phone, card);
            // 设置ajax参数
            let api = "http://newshopapi.0melon0.cn/api/f_user/register"
            let data = {
                    "name": name,
                    "phone": phone,
                    "card": card
                }
                // 请求数据
            ajax(api, data, "POST").then(res => {
                // 存储数据
                sessionStorage.setItem("token", res.result.token);
                sessionStorage.setItem("name", name);
                sessionStorage.setItem("phone", phone);
                sessionStorage.setItem("card", card);
                location.href = "check.html";
            }).catch(err => {
                console.log(err);
            })
        });
    }
})



// 封装请求ajax函数
function ajaxFn() {
    // 获取用户输入信息
    let name = nameMsg.value;
    let phone = phoneMsg.value;
    let card = idMsg.value;
    console.log(name, phone, card);
    // 设置ajax参数
    let api = "http://newshopapi.0melon0.cn/api/f_user/register"
    let data = {
            "name": name,
            "phone": phone,
            "card": card
        }
        // 请求数据
    ajax(api, data, "POST").then(res => {
        // 存储数据
        // console.log(res);
        sessionStorage.setItem("token", res.result.token);
        sessionStorage.setItem("name", name);
        sessionStorage.setItem("phone", phone);
        sessionStorage.setItem("card", card);
        // console.log(res.result.token);
        // console.log(sessionStorage.getItem("token"));
    }).catch(err => {
        console.log(err);
    })
}